// pages/order/order.js
const app = getApp()
var request = require("../../utils/request.js");
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    statusBarHeight: app.globalData.statusBarHeight,
    windowHeight: 0,
    scrollViewHeight: 0,
    tabs: [{
      id: 0,
      name: '已预约',
      select: true
    },
    {
      id: 1,
      name: '计费中',
      select: false
    },
    {
      id: 2,
      name: '已结束',
      select: false
    },
    ],
    subscribeList: [],
    chargeList: [],
    completedList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let tabIndex = options.tabIndex;
    let tabs = that.data.tabs;
    if (tabIndex != '' && tabIndex != undefined) {
      for (let i = 0; i < tabs.length; i++) {
        if (i == tabIndex) {
          tabs[i].select = true
        } else {
          tabs[i].select = false;
        }
      }
      that.setData({
        tabs: tabs,
        tabIndex: tabIndex
      })
      //根据tab切换获取当前列表
      if (tabIndex == 0) {
        that.getSubscribeList();
      } else if (tabIndex == 1) {
        that.getChargeList();
      } else {
        that.getCompletedList();
      }
    }
    // 先取出页面高度 windowHeight
    that.setData({
      windowHeight: wx.getSystemInfoSync().windowHeight
    });
    let scrollViewHeight = that.data.windowHeight - that.data.statusBarHeight - 44 - 27 - 22 - 10;
    that.setData({
      scrollViewHeight: scrollViewHeight
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getSubscribeList();
    this.getChargeList();
    this.getCompletedList();
  },
  /**
   * 切换tab
   */
  selectTab(e) {
    let index = e.target.dataset.index;
    let that = this;
    let tabs = that.data.tabs;
    for (let i = 0; i < tabs.length; i++) {
      if (i == index) {
        tabs[i].select = true
      } else {
        tabs[i].select = false;
      }
    }
    that.setData({
      tabIndex: index,
      tabs: tabs
    })
    //根据tab切换获取当前列表
    if (index == 0) {
      that.getSubscribeList();
    } else if (index == 1) {
      that.getChargeList();
    } else {
      that.getCompletedList();
    }
  },
  /**
   * 导航
   * @param {*} event 
   */
  openLocation(event) {
    let index = event.currentTarget.dataset.index;
    let that = this;
    let subscribeList = that.data.subscribeList;
    let latitude = Number(subscribeList[index].latitude)
    let longitude = Number(subscribeList[index].longitude)
    wx.openLocation({
      latitude,
      longitude,
      name: subscribeList[index].parkName,
      address: subscribeList[index].parkAddress,
      scale: 28
    })

  },
  /**
   * 获取预约列表
   */
  getSubscribeList() {
    let that = this;
    let userId = wx.getStorageSync('userId');
    let url = "/api/cUser/mySubscribe?userId=" + userId;
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          let subscribeList = res.data.data;
          if (subscribeList != null && subscribeList.length > 0) {
            for (let i = 0; i < subscribeList.length; i++) {
              let createTime = subscribeList[i].createTime;
              subscribeList[i].createTime = subscribeList[i].createTime.substring(0, 10);
              subscribeList[i].subscribeTime = subscribeList[i].subscribeTime.substring(0, 16);
            }
            that.setData({
              subscribeList: subscribeList
            })
          } else {
            that.setData({
              subscribeList: []
            })
          }
        }
      })
  },
  /**
   * 获取计费中列表
   */
  getChargeList() {
    let that = this;
    let userId = wx.getStorageSync('userId');
    let url = "/api/endCar/endCarhIn?uid=" + userId;
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          let chargeList = res.data.data;
          if (chargeList != null && chargeList.length > 0) {
            for (let i = 0; i < chargeList.length; i++) {
              chargeList[i].carInTime = chargeList[i].carInTime.substring(11, 16);
              if (chargeList[i].dTimes > 0) {
                chargeList[i].dTimes = that.getDuration(chargeList[i].dTimes);
              } else {
                chargeList[i].dTimes = 0;
              }
              if (chargeList[i].parkCost == null) {
                chargeList[i].parkCost = '暂无'
              }
              chargeList[i].createTime = chargeList[i].createTime.substring(0, 10);
            }
            that.setData({
              chargeList: chargeList
            })
          } else {
            that.setData({
              chargeList: []
            })
          }
        }
      })
  },
  /**
   * 获取已结束列表
   */
  getCompletedList() {
    let that = this;
    let userId = wx.getStorageSync('userId');
    let url = "/api/endCar/endCar?uid=" + userId;
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          let completedList = res.data.data;
          if (completedList != null && completedList.length > 0) {
            for (let i = 0; i < completedList.length; i++) {
              if (completedList[i].carOutTime != null) {
                completedList[i].carOutTime = completedList[i].carOutTime.substring(11, 16);
              } else {
                completedList[i].carOutTime = '暂无'
              }
              completedList[i].carInTime = completedList[i].carInTime.substring(11, 16);
              completedList[i].payTime = completedList[i].payTime.substring(0, 10);
            }
            that.setData({
              completedList: completedList,
            })
          } else {
            that.setData({
              completedList: []
            })
          }
        }
      })
  },
  /**
   * 删除订单
   * @param {*} event 
   */
  deleteOrder(event) {
    let that = this;
    let orderId = event.currentTarget.dataset.id;
    let url = "/api/endCar/deleteByOrderId?orderId=" + orderId;
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          Toast.success('删除成功');
          that.getCompletedList();
        } else {
          Toast.fail('删除失败');
        }
      })
      .catch((errMsg) => {
        // wx.showToast({
        //   title: '删除失败',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  },
  /**
   * 预约取消
   * @param {*} event 
   */
  subscribeCancel(event) {
    let subscribeId = event.currentTarget.dataset.subscribeid;
    let that = this;
    let userId = wx.getStorageSync('userId');
    let url = "/api/cSubscribe/cancelReservation?userId=" + userId + "&subscribeId=" + subscribeId;
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          Toast.success('取消成功');
          that.getSubscribeList();
        } else {
          Toast.fail('取消失败');
        }
      })
      .catch((errMsg) => {
        // wx.showToast({
        //   title: '取消失败',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  },
  /**
   * 跳转到订单详情
   * @param {*} event 
   */
  toOrderInfo(event) {
    let carCardNum = event.currentTarget.dataset.carnumber;
    wx.navigateTo({
      url: '../payForm/payForm?carCardNum=' + carCardNum,
    })
  },
  /**
   * 将毫秒转化
   * @param {String} my_time 毫秒时间
   */
  getDuration(my_time) {
    var days = my_time / 1000 / 60 / 60 / 24;
    var daysRound = Math.floor(days);
    var hours = my_time / 1000 / 60 / 60 - (24 * daysRound);
    var hoursRound = Math.floor(hours);
    var minutes = my_time / 1000 / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
    var minutesRound = Math.floor(minutes);
    var seconds = my_time / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
    if (daysRound != '' && daysRound > 0) {
      var time = daysRound + '天' + hoursRound + '小时' + minutesRound + '分钟';
    } else if (daysRound == 0 && hoursRound != '' && hoursRound > 0) {
      var time = hoursRound + '小时' + minutesRound + '分钟';
    } else if (hoursRound == 0 && minutesRound != '' && minutesRound > 0) {
      var time = minutesRound + '分钟';
    } else if (minutesRound == 0 && seconds != '' && seconds > 0) {
      seconds = seconds.toString();
      seconds = seconds.substring(0, 2);
      var time = seconds + '秒钟';
    }
    return time;
  },
})